package cn.iocoder.mall.user.application.controller.users;

import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.utils.common.api.vo.Result;
import cn.iocoder.common.utils.common.system.util.JwtUtil;
import cn.iocoder.mall.user.api.UserProductSpuCollectionsService;
import cn.iocoder.mall.user.api.bo.MemberShopCollectionBO;
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsPageBO;
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsPageDTO;
import cn.iocoder.mall.user.biz.service.IMemberShopCollectionService;
import cn.iocoder.mall.user.sdk.annotation.RequiresLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户收藏
 * @author xiaofeng
 * @date 2019/07/07 11:06
 * @version 1.0
 */

@RestController
@RequestMapping("users/favorite")
@Api("用户收藏")
public class UserFavoriteController {

    @Reference(validation = "true", version = "${dubbo.provider.UserProductSpuCollectionsService.version}")
    private UserProductSpuCollectionsService userProductSpuCollectionsService;

    @Autowired
    private IMemberShopCollectionService shopCollectionService;

    @GetMapping("/product/page")
    @RequiresLogin
    @ApiOperation("用户商品收藏列表")
    public CommonResult<UserProductSpuCollectionsPageBO> getUserProductSpuCollectionsPage(
            @Validated UserProductSpuCollectionsPageDTO userProductSpuCollectionsPageDTO) {
        userProductSpuCollectionsPageDTO.setUserId(JwtUtil.getUserIdByToken());
        return userProductSpuCollectionsService.getUserProductSpuCollectionsPage(userProductSpuCollectionsPageDTO);
    }

    @DeleteMapping("/product/remove")
    @RequiresLogin
    @ApiOperation(value = "用户商品收藏-删除")
    public CommonResult<Boolean> removeUserFavorite(@RequestParam("spuId") final Long spuId) {
        return userProductSpuCollectionsService.deleteUserProductSpuCollections(JwtUtil.getUserIdByToken(), spuId);
    }

    @GetMapping("/product/hasUserFavorite")
    @RequiresLogin
    @ApiOperation(value = "用户商品收藏-是否收藏")
    public CommonResult<Boolean> hasUserSpuFavorite(@RequestParam("spuId") final Long spuId) {
        return userProductSpuCollectionsService.hasUserSpuFavorite(spuId, JwtUtil.getUserIdByToken());
    }

    @GetMapping("/shop/page")
    public CommonResult<?> getShopCollectionPage(@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam (defaultValue = "1")Integer pageNo){
        Page page=new Page().setSize(pageSize).setCurrent(pageNo);
       return CommonResult.success(shopCollectionService.getListByUserId(page,JwtUtil.getUserIdByToken()));
    }
    @DeleteMapping("/shop/remove")
    public CommonResult<?> removeUserFavorite(@RequestParam String shopId){
       return CommonResult.success(shopCollectionService.removeUserFavorite(JwtUtil.getUserIdByToken(),shopId));
    }
    @GetMapping("/shop/hasUserFavorite")
    public CommonResult<?> hasUserFavorite(@RequestParam String shopId){

        return CommonResult.success(shopCollectionService.hasUserSpuFavorite(JwtUtil.getUserIdByToken(),shopId));
    }


}
